
package com.yunfei.api.fengXianWenJuanDiaoCha;

import com.yunfei.api.fengXianWenJuanDiaoCha.param.SaveAnswerParam;
import com.yunfei.api.fengXianWenJuanDiaoCha.param.SelectAnswerParam;
import com.yunfei.bo.product.ProductDetailBo;
import com.yunfei.bo.question.AnswerBo;
import com.yunfei.bo.question.QuestionBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.QuestionMapper;
import com.yunfei.utils.SnowflakeIdWorker;
import com.yunfei.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 风险问卷调查
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "风险问卷调查(投资者)",tags = "风险问卷调查")
@RestController
@RequestMapping("/api/fx/quesion")
public class FxQuesionApiController {
    @Autowired
    private QuestionMapper questionMapper;
    /**
    * @Description: 如果客户点击同意，更新表：
     * UPDATE T_PERSONAL_USER SET INVE_COMM_LETER_FLG = 'Y' --当点击同意时，更新字段为Y，点击不同意，更新为N
     * WHERE MOBILE = '18810275220';
     * COMMIT;
    * @Author: zhengyunfei
    * @Date: 2019/1/11
    * @time:17:00
    */
    @PostMapping("/update")
    @ApiOperation(value = "投资承若涵",notes = "投资承若涵,当客户点击确定的时候，调用此接口",position = 1,response = Object.class)
    @ResponseBody
    public Object update(String token){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        String userId= TokenUtil.getInstance().getUserId(token);
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("userId",userId);
        try{
            questionMapper.updateChengRuoHan(paramMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    @GetMapping("/get")
    @ApiOperation(value = "风险调查问卷",notes = "风险调查问卷,获取所有问题以及选项",position = 1,response = Object.class)
    @ResponseBody
    public Object get(){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> paramMap=new HashMap<>();
        try{
            List<QuestionBo> questionBoList=questionMapper.getQuestionList(paramMap);
            for(int i=0;i<questionBoList.size();i++){
                String questionId=questionBoList.get(i).getQuestionId();
                Map<String,Object> optionMap=new HashMap<>();
                optionMap.put("quesId",questionId);
                List<AnswerBo> answerBoList=questionMapper.getAnswerList(optionMap);
                questionBoList.get(i).setAnswerList(answerBoList);
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(questionBoList);
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    @PostMapping("/save")
    @ApiOperation(value = "风险调查问卷",notes = "风险调查问卷,保存所选择的问题选项",position = 1,response = Object.class)
    @ResponseBody
    public Object save(SelectAnswerParam param){
        ResultModel resultModel=new ResultModel();
        String token=param.getToken();
        String userId=TokenUtil.getInstance().getUserId(token);
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> paramMap=new HashMap<>();
        Map<String,Object> scoreMap=new HashMap<>();
        try{
              String questionId=param.getQuestionId();
              String optionId=param.getOptionId();
              String questionIdArray[]=questionId.split(",");
              paramMap.put("optionIds",optionId.split(","));
              String optionIdArray[]=optionId.split(",");
              for(int i=0;i<questionIdArray.length;i++){
                  SaveAnswerParam saveAnswerParam=new SaveAnswerParam();
                  saveAnswerParam.setQuestionId(questionIdArray[i]);
                  saveAnswerParam.setOptionId(optionIdArray[i]);
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                  String userAnswerId= sdf.format(new Date());
                  SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
                  userAnswerId= idWorker.nextId()+"";
                  saveAnswerParam.setUserAnswerId(userAnswerId);
                  questionMapper.add(saveAnswerParam);
            }
            //查询分数
            int scroe=questionMapper.getScore(paramMap);
              //更新分数
            Map<String,Object> updateScoreParam=new HashMap<>();
            updateScoreParam.put("userId",userId);
            updateScoreParam.put("score",scroe);
            questionMapper.updateScore(updateScoreParam);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
           // resultModel.setData(questionBoList);
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
        }
        return resultModel;
    }
    public static void main(String args []){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String userAnswerId= sdf.format(new Date());
        System.out.println(  userAnswerId);
    }
}
